module py2Obj {
    export class MathUtils {
        public static getCrossPointBetweenCircleAndLine(cx: number, cy: number, r: number, stx: number, sty: number, edx: number, edy: number): { x, y } {
            // 求直线
            var k = (edy - sty) / (edx - stx);
            var b = edy - k * edx;
            //列方程
            var x1, y1, x2, y2;
            var c = cx * cx + (b - cy) * (b - cy) - r * r;
            var a = (1 + k * k);
            var b1 = (2 * cx - 2 * k * (b - cy));

            var tmp = Math.sqrt(b1 * b1 - 4 * a * c);
            x1 = (b1 + tmp) / (2 * a);
            y1 = k * x1 + b;
            x2 = (b1 - tmp) / (2 * a);
            y2 = k * x2 + b;
            //判断求出的点是否在圆上
            var res = (x1 - cx) * (x1 - cx) + (y1 - cy) * (y1 - cy);
            var p = { x: 0, y: 0 };
            if (res == r * r) {

                p.x = x1;
                p.y = y1;
            } else {
                p.x = x2;
                p.y = y2;
            }
            return p;
        }
        public static getFootfallPoint(stx: number, sty: number, edx: number, edy: number, x: number, y: number) {
            var k = (edy - sty) / (edx - stx);
            var b = edy - k * edx;
            let m = x + k * y;
            let pt = { x: 0, y: 0 };
            pt.x = (m - k * b) / (k * k + 1);
            pt.y = (k * pt.x) + b;
            return pt;
        }

        public static getDistanceBetweenPointAndLine(x1: number, y1: number, x2: number, y2: number, x: number, y: number, body: GameControl) {
           // console.log("&&&&&&&&&&&&&&&" + x1 + "," + y1 + "," + x2 + "," + y2 + "," + x + "," + y);
            let pt = MathUtils.getFootfallPoint(x1, y1, x2, y2, x, y);
            /*  var shape = new egret.Shape();
               shape.graphics.lineStyle(2, 0xff0000);
             shape.graphics.drawCircle(pt.x, pt.y, 10);
               shape.graphics.endFill();
               body.addChild(shape);
               let k= (y1-y2)/(x1-x2);
               let b = y1 -k*y1;
               /// > 0 = ax +b -y;  
              */
            return Math.sqrt((x - pt.x) * (x - pt.x) + (y - pt.y) * (y - pt.y));
        }

        static EPS: number = 1e-9;//误差，小于这个就当作0
        /// <summary>
        /// 线段与圆的交点
        /// </summary>
        /// <param name="ptStart">线段起点</param>
        /// <param name="ptEnd">线段终点</param>
        /// <param name="ptCenter">圆心坐标</param>
        /// <param name="Radius2">圆半径平方</param>
        /// <param name="ptInter1">交点1(若不存在返回65536)</param>
        /// <param name="ptInter2">交点2(若不存在返回65536)</param>
        public static LineInterCircle(ptStart: { x: number, y: number }, ptEnd: { x: number, y: number }, ptCenter: { x: number, y: number }, Radius: number,
            ptInter1: { x: number, y: number }, ptInter2: { x: number, y: number }) {
            ptInter1.x = ptInter2.x = 65536;
            ptInter2.y = ptInter2.y = 65536;
            let fDis = Math.sqrt((ptEnd.x - ptStart.x) * (ptEnd.x - ptStart.x) + (ptEnd.y - ptStart.y) * (ptEnd.y - ptStart.y));

            let d = {x:0,y:0};
            d.x = (ptEnd.x - ptStart.x) / fDis;
            d.y = (ptEnd.y - ptStart.y) / fDis;

            let E = {x:0,y:0};
            E.x = ptCenter.x - ptStart.x;
            E.y = ptCenter.y - ptStart.y;

            let a = E.x * d.x + E.y * d.y;
            let a2 = a * a;

            let e2 = E.x * E.x + E.y * E.y;

            let r2 = Radius * Radius;

            if ((r2 - e2 + a2) < 0) {
                return false;
            }
            else {
                let f = Math.sqrt(r2 - e2 + a2);

                let t = a - f;

           
                    ptInter1.x = ptStart.x + t * d.x;
                    ptInter1.y = ptStart.y + t * d.y;


                t = a + f;


                    ptInter2.x = ptStart.x + t * d.x;
                    ptInter2.y = ptStart.y + t * d.y;

                return true;
            }
        }

    }
}
